<?php

namespace App\Http\Controllers\Api;

use App\ForumPost;
use App\ForumSort;
use App\ForumZan;
use App\Http\Requests\ForumPostStore;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class ForumPostController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $orderBy = $request->order_by ? $request->order_by : 'id';
        $orderType = $request->order_type ? $request->order_type : 'desc';
        $pageSize = $request->page_size ? $request->page_size : $this->pageSize;
        $lists = ForumPost::orderBy($orderBy, $orderType)->with(['sort', 'user', 'zan' => function ($query) {
            return $query->where('user_id', Auth::id());
        }]);
        if ($request->get('sort_id')) {
            $lists = $lists->where('sort_id', $request->sort_id);
        }
        if ($request->get('user_id')) {
            $lists = $lists->where('user_id', $request->user_id);
        }
        if ($request->get('title')) {
            $lists = $lists->where('title', $request->title);
        }
        $list = $lists->paginate($pageSize);
        return $list;
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(ForumPostStore $request)
    {
        $data = [];
        if ($request->get('content')) {
            $data['content'] = $request->get('content');
        }
        if ($request->get('image')) {
            $data['image'] = implode(',', $request->get('image'));//图片路径用','号分割
        }
        if (!isset($data['content']) && !isset($data['image'])) {
            return response(['message' => '帖子不能没有内容哦'], 400);
        }
        $data['user_id'] = Auth::id();
        $data['title'] = $request->title;
        $data['sort_id'] = $request->sort_id;
        if (!ForumSort::find($request->sort_id)) {
            return response(['message' => '帖子分类错误'], 400);
        }
        $res = ForumPost::create($data);
        if ($res) {
            return response(['message' => '发表成功']);
        }
        return response(['message' => '服务器繁忙'], 400);
    }

    /**
     * @param ForumPost $forumPost
     * @return ForumPost
     */
    public function show(ForumPost $forumPost)
    {
        //
    }

    public function look($id)
    {
        $info = ForumPost::where('id', $id)->with(['user', 'sort', 'comment.userInfo'])->first();
        if ($info->image) {
            $info->image = explode(',', $info->image);
        }
        //我是否已经点赞
        $info->is_zan = 0;
        if (ForumZan::where('user_id', Auth::id())->where('post_id', $id)->first()) {
            $info->is_zan = 1;
        }
        return $info;
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\ForumPost $forumPost
     * @return \Illuminate\Http\Response
     */
    public function edit(ForumPost $forumPost)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \App\ForumPost $forumPost
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, ForumPost $forumPost)
    {
        $forumPost->update($request->only(['status']));
        return response(['message' => '修改成功']);
    }

    /**
     * @param ForumPost $forumPost
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     * @throws \Exception
     */
    public function destroy(ForumPost $forumPost)
    {
        //软删除
        $forumPost->delete();
        return response(['message' => '删除成功']);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     * 点赞和取消点赞
     */
    public function zan(Request $request)
    {
        $info = ForumZan::where('user_id', Auth::id())->where('post_id', $request->post_id)->first();
        if ($info) {
            DB::table('forum_posts')->where('id', $request->post_id)->decrement('zan_num');
            $info->delete();
            return response(['is_zan' => 0]);
        } else {
            ForumZan::create([
                'user_id' => Auth::id(),
                'post_id' => $request->post_id,
            ]);
            DB::table('forum_posts')->where('id', $request->post_id)->increment('zan_num');
            return response(['is_zan' => 1]);
        }
    }
}
